package gold.digger;

import gold.vo.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC144 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> res = new ArrayList<>();

            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            TreeNode cur = root;
            while (!stack.isEmpty() && null != cur) {
                cur = stack.pop();
                res.add(cur.val);

                if (null != cur.right) stack.push(cur.right);
                if (null != cur.left) stack.push(cur.left);
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode root = TreeNode.createTree("1,2,3,4,5");
        System.out.println(solution.preorderTraversal(null));
    }

    public static void main(String[] args) throws Exception {
        LC144 an = new LC144();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
